import Parser=require('./../base/Parser');
import GCJ_CLASS_TYPE = require('./../../common/GCJ_CLASS_TYPE');
import Serializer = require('./../base/Serializer');
import TextExtObj = require('./../obj/TextExtObj');
import SJGReplaceItemBase = require('./SJGReplaceItemBase');
import SJGReplaceItemTitleText = require('./SJGReplaceItemTitleText');
import CommonModel = require('./../../common/model/CommonModel');


var CONFIG=require('../../../../common/config');
export  = SJGSubGroupText;

/**
 * 文字数组(普通正文／小标题)
 * @author 
 *
 */
class SJGSubGroupText extends SJGReplaceItemBase{
    
    //  数组内容
    public value: Array<SJGReplaceItemTitleText> = new Array<SJGReplaceItemTitleText>();
    
    public constructor(g?) {
        super(g);

        this.ct = GCJ_CLASS_TYPE.CLASS_TYPE_SJGSubGroupText;
        this.type = SJGReplaceItemBase.TYPE_SUBGROUP_TEXT;
    }
    
    // 从json对象(注意不是json字符串)中解析
    public fromJson(jsonObj,versionId) {
        super.fromJson(jsonObj,versionId);
        
        this.value.splice(0,this.value.length);
        if(jsonObj.hasOwnProperty('value')) {
            for(var i = 0;i < jsonObj.value.length;i++) {
//                var item: SJGReplaceItemTitleText = Parser.fromUnknownJson(jsonObj.value[i],versionId,this.getGlobalData());
                var item: SJGReplaceItemTitleText=new SJGReplaceItemTitleText();
                item.fromJson(jsonObj.value[i],versionId);
                this.value.push(item);
            }
        }
    }
    
    // 转成json字符串
    public toJson(): string {
        // 去掉父json字符串的头尾
        var superJson = super.toJson();
        superJson = superJson.substr(1,superJson.length - 2);

        var ret = '{';
        ret += superJson;
        
        // value
        ret += ',';
        ret += '"value":[';
        for (var i = 0;i < this.value.length;i++) {
            if(i > 0) {
                ret += ',';
            }
            ret += this.value[i].toJson();
        }
        ret += ']',
        
        ret += '}';
        return ret;
    }
    
    // 转换成CommonModel树
    public toCommonModel(level: number): CommonModel {
        var ret: CommonModel=CommonModel.from(this.id,this.getObjetName(),CommonModel.SPREAD_COLLAPSE,level);

        for(var i=0;i<this.value.length;i++) {
            var r: SJGReplaceItemBase=this.value[i];
            var s: CommonModel=r.toCommonModel(level+1);
            ret.addChild(s);
        }
        return ret;

    }
    // 获取对象名称
    public getObjetName(): string {
        return "文字2";
    }
    // 参数：updateOnce［可选］，true表示只发生一次替换
    public updateReplaceItemId(oldId,newId,updateOnce?: Boolean): Boolean {
        // 先处理子，再处理自身
        for(var i=0;i<this.value.length;i++) {
            var r: SJGReplaceItemBase=this.value[i];
            if(r.updateReplaceItemId(oldId,newId, updateOnce)){
                return true;
            }
        }
        if(super.updateReplaceItemId(oldId,newId,updateOnce)) {
            return true;
        }
        
        return false;
    }
    
    // 获取小标题
    public getTitleItem(): SJGReplaceItemTitleText {
        for(var i=0;i<this.value.length;i++) {
            var item: SJGReplaceItemTitleText=this.value[i];
            if(item.tt==TextExtObj.TEXT_TYPE_TITLE) {// 小标题
                return item;
            }
        }
        return null;
    }
    // 获取正文
    public getTextItem(): SJGReplaceItemTitleText {
        for(var i=0;i<this.value.length;i++) {
            var item: SJGReplaceItemTitleText=this.value[i];
            if(item.tt==TextExtObj.TEXT_TYPE_BODY) {// 正文
                return item;
            }
        }
        return null;
    }
    
//SJG_MARK_APPEND--------------------
    public isContentEmpty():boolean {
        if(this.value.length==0)    return true;
        for(var i=0;i<this.value.length;i++){
            if(!this.value[i].isContentEmpty()){
                return false;
            }
        }
        return true;
    }

}//SJG_MARK_END-------------------------